<%--
功能：货品图片
--%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ taglib prefix="product" tagdir="/WEB-INF/tags/product"%>
<%@ tag import="org.springframework.context.ApplicationContext" %>
<%@ tag import="org.springframework.web.context.support.WebApplicationContextUtils" %>
<%@ tag import="com.probiz.estore.product.service.ProductSkuManager" %>
<%@ tag import="com.probiz.estore.common.model.product.ProductSku" %>
<%@ tag import="org.apache.commons.lang.StringUtils;" %>

<%@ attribute name="productSku" type="com.probiz.estore.common.model.product.ProductSku" required="false" description="货品"%>
<%@ attribute name="productSkuId" type="java.lang.Integer" required="false" description="货品id 如果productSku 无指定,则可用这个"%>
<%@ attribute name="productSkuCode" type="java.lang.String" required="false" description="货品code.如果productSku,productSkuId无指定，则必须指定productSkuCode" %>
<%@ attribute name="linkType" type="java.lang.Short"  description="0表示没有链接，1表示链接去商品页，2表示打开新窗口查看原图，不填表示1"%>
<%@ attribute name="sizeType" type="java.lang.String" description="s0=(58X58) s1=(115X115); s2=(170X170); m0=(370X370); (s代表小，m代表中)不填代表显示的是原图，尺寸不限" %>
<%@ attribute name="width" %>
<%@ attribute name="height" %>
<%@ attribute name="style" %>
<%@ attribute name="linkClass" %>
<%@ attribute name="id" %>
<%@ attribute name="alt" %>
<%@ attribute name="onmouseover" %>
<%@ attribute name="onclick" %>
<%
	ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(session.getServletContext());
	ProductSkuManager productSkuManager = (ProductSkuManager) ctx.getBean("productSkuManager");
	ProductSku targetProductSku = productSku;
	try{
		if(null == targetProductSku){
			if(null != productSkuId){
				targetProductSku = productSkuManager.getById(productSkuId);
			}else if(null != productSkuCode){
				targetProductSku = productSkuManager.getProductSkuByProductSkuCode(productSkuCode);
			}else{
				//构造一个虚拟productSku
				targetProductSku = new ProductSku();
			}
		
		}
	}catch(Exception e){
		e.printStackTrace();
	}
	request.setAttribute("targetProductSku",targetProductSku);
	
	String url = targetProductSku.getImageUrl();
	if(StringUtils.isBlank(url)&& targetProductSku.getProduct()!=null){
		url = targetProductSku.getProduct().getImageUrl();
	}
	if(null != url){
		if(StringUtils.isNotBlank(sizeType)){
			int index = url.lastIndexOf(".");
			if(index!=-1)url = url.substring(0, index) + "_"+sizeType + url.substring(index);
		}
		jspContext.setAttribute("imageUrl",url);
		jspContext.setAttribute("linkImageUrl",url);
	}else{
		jspContext.setAttribute("imageType","_s0");
		jspContext.setAttribute("imageUrl","");
		jspContext.setAttribute("linkImageUrl","");
	}	
%>
<c:choose>
	<c:when test="${linkType==0}">
	</c:when>
	<c:when test="${empty linkType or linkType==1}">
		<a href="<product:productUrl product="${targetProductSku.product}"/>" <c:if test="${not empty linkClass}">class="${linkClass}"</c:if>> 
	</c:when>
	<c:when test="${linkType==2}">
		<c:choose>
			<c:when test="${not empty linkImageUrl}">
				<a href="${mediaPath}${linkImageUrl}" target="blank">
			</c:when>
			<c:otherwise>
				<a href="javascript:void(0);"><!--重要：上传图后要把a的target设为_blank  -->
			</c:otherwise>
		</c:choose>
		
	</c:when>
</c:choose>
<img src="${mediaPath}${imageUrl}" onerror="$j(this).attr('originsrc',this.src);this.src=__defaultImage${(not empty sizeType)?'_':''}${sizeType};this.onerror='';" originsrc=""
<c:if test="${not empty width}">width="${width}"</c:if>
<c:if test="${not empty height}">height="${height}"</c:if>
<c:if test="${not empty style}">style="${style}"</c:if>
<c:if test="${not empty id}">id="${id}"</c:if>
<c:if test="${not empty alt}">alt="${alt}"</c:if>
<c:if test="${not empty onmouseover}">onmouseover="${onmouseover}" </c:if>
<c:if test="${not empty onclick}">onclick="${onclick}" </c:if> />
<c:if test="${empty linkType or linkType!=0}"></a></c:if>



